package com.company.cloud.club.modules.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.company.cloud.club.modules.constants.AliyunOSSConstant;
import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.net.URL;
import java.util.Date;

/**
 * *****************************************************************************
 * <p>
 * 功能名           ：
 * 系统名           ：
 * <p>
 * *****************************************************************************
 * Modification History
 * <p>
 * Date        Name                    Reason for Change
 * ----------  ----------------------  -----------------------------------------
 * 2018/4/24      zhangxu                 Created
 */
public class FileUtil {

    /**
     * base64字符串转文件
     * 对字节数组字符串进行Base64解码并生成图片
     */
    public static File GenerateImage(String base, String prefix, String suffix) {
        File file = null;
        try {
            file = File.createTempFile(prefix, "." + suffix);
            byte[] buffer = Base64.decodeBase64(base);
            OutputStream out = new FileOutputStream(file);
            out.write(buffer);
            out.flush();
            out.close();
        } catch (Exception e) {

        }
        return file;
    }

    public static void uploadByBytes(String path, String filename, byte[] content) {

        uploadByBytes(AliyunOSSConstant.BUCKET_NAME, path, filename, content);

    }

    public static void uploadByBytes(String bucket, String path, String filename, byte[] content) {

        OSS ossClient = new OSSClientBuilder().build(AliyunOSSConstant.END_POINT, AliyunOSSConstant.ACCESS_KEY_ID, AliyunOSSConstant.ACCESS_KEY_SECRET);

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, path + filename, new ByteArrayInputStream(content));

        ossClient.putObject(putObjectRequest);

        ossClient.shutdown();

    }

    public static String getDownloadUrl(String fileName) {

        OSS ossClient = new OSSClientBuilder().build(AliyunOSSConstant.END_POINT, AliyunOSSConstant.ACCESS_KEY_ID, AliyunOSSConstant.ACCESS_KEY_SECRET);

        Date expiration = new Date(new Date().getTime() + 3600 * 1000);

        URL url = ossClient.generatePresignedUrl(AliyunOSSConstant.BUCKET_NAME, fileName, expiration);

        ossClient.shutdown();

        return AliyunOSSConstant.OSS_HOST + url.getFile();
    }

    public static void downloadFile(String fileName) {

        OSS ossClient = new OSSClientBuilder().build(AliyunOSSConstant.END_POINT, AliyunOSSConstant.ACCESS_KEY_ID, AliyunOSSConstant.ACCESS_KEY_SECRET);

        ossClient.getObject(new GetObjectRequest(AliyunOSSConstant.BUCKET_NAME, fileName), new File("/alidata/server/www/qxzychina/qxzychina-admin-web/html/resource/" + fileName));

        ossClient.shutdown();

    }

    public static byte[] readInputStream(InputStream inputStream) throws Exception {
        byte[] buffer = new byte[1024];
        int len = -1;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.close();
        inputStream.close();
        return outputStream.toByteArray();
    }

}
